using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The GuardianContactTableDB class is responsible for interacting with the database to retrieve and store information
    /// about GuardianContactTable objects.
    /// </summary>
    public class GuardianContactTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of GuardianContactTable from the underlying datasource.
        /// </summary>
        /// <param name="guardianContactKey">The unique GuardianContactKey of the GuardianContactTable in the database.</param>
        /// <returns>An GuardianContactTable when the GuardianContactKey was found in the database, or null otherwise.</returns>
        public static GuardianContactTable GetItem(int guardianContactKey)
        {
            GuardianContactTable myGuardianContactTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocGuardianContactTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@guardianContactKey", guardianContactKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myGuardianContactTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myGuardianContactTable;
            }
        }


        /// <summary>
        /// Gets an instance of GuardianContactTable from the underlying datasource.
        /// </summary>
        /// <param name="UserAccountKey">The unique UserAccountKey of the GuardianContactTable in the database.</param>
        /// <returns>An GuardianContactTable when the UserAccountKey was found in the database, or null otherwise.</returns>
        public static GuardianContactTable GetItemByUserAccountKey(int UserAccountKey)
        {
            GuardianContactTable myGuardianContactTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocGuardianContactTableSelectSingleItemByUserAccountKey", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@UserAccountKey", UserAccountKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myGuardianContactTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myGuardianContactTable;
            }
        }

        /// <summary>
        /// Returns a list with GuardianContactTable objects.
        /// </summary>
        /// <returns>A generics List with the GuardianContactTable objects.</returns>
        public static GuardianContactTableList GetList()
        {
            GuardianContactTableList tempList = new GuardianContactTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocGuardianContactTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Returns a list with GuardianContactTable objects.
        /// </summary>
        /// <returns>A generics List with the GuardianContactTable objects.</returns>
        public static GuardianContactTableList GetListByUserAccountKey(int UserAccountKey)
        {
            GuardianContactTableList tempList = new GuardianContactTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocGuardianContactTableSelectListByUserAccountKey", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@userAccountKey", UserAccountKey);
                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
                return tempList;
            }
        }

        /// <summary>
        /// Saves a GuardianContactTable in the database.
        /// </summary>
        /// <param name="myGuardianContactTable">The GuardianContactTable instance to save.</param>
        /// <returns>The new GuardianContactKey if the GuardianContactTable is new in the database or the existing GuardianContactKey when an item was updated.</returns>
        public static int Save(GuardianContactTable myGuardianContactTable)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocGuardianContactTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myGuardianContactTable.GuardianContactKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@guardianContactKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@guardianContactKey", myGuardianContactTable.GuardianContactKey);
                    }
                    if (myGuardianContactTable.UserAccountKey == null)
                    {
                        myCommand.Parameters.AddWithValue("@userAccountKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@userAccountKey", myGuardianContactTable.UserAccountKey);
                    }
                    if (string.IsNullOrEmpty(myGuardianContactTable.MobileNumber))
                    {
                        myCommand.Parameters.AddWithValue("@mobileNumber", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@mobileNumber", myGuardianContactTable.MobileNumber);
                    }
                    if (string.IsNullOrEmpty(myGuardianContactTable.HomePhoneNumber))
                    {
                        myCommand.Parameters.AddWithValue("@homePhoneNumber", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@homePhoneNumber", myGuardianContactTable.HomePhoneNumber);
                    }
                    if (string.IsNullOrEmpty(myGuardianContactTable.FaxNumber))
                    {
                        myCommand.Parameters.AddWithValue("@FaxNumber", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@FaxNumber", myGuardianContactTable.FaxNumber);
                    }
                    if (string.IsNullOrEmpty(myGuardianContactTable.OfficePhoneNumber))
                    {
                        myCommand.Parameters.AddWithValue("@OfficePhoneNumber", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@OfficePhoneNumber", myGuardianContactTable.OfficePhoneNumber);
                    }
                    if (string.IsNullOrEmpty(myGuardianContactTable.OtherNumber))
                    {
                        myCommand.Parameters.AddWithValue("@otherNumber", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@otherNumber", myGuardianContactTable.OtherNumber);
                    }
                    if (string.IsNullOrEmpty(myGuardianContactTable.EmailAddress))
                    {
                        myCommand.Parameters.AddWithValue("@emailAddress", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@emailAddress", myGuardianContactTable.EmailAddress);
                    }

                    myCommand.Parameters.AddWithValue("@status", myGuardianContactTable.Status);

                    //if (myGuardianContactTable.CreatedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", myGuardianContactTable.CreatedDateTime);
                    //}
                    //if (myGuardianContactTable.ModifiedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", myGuardianContactTable.ModifiedDateTime);
                    //}

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a GuardianContactTable from the database.
        /// </summary>
        /// <param name="guardianContactKey">The GuardianContactKey of the GuardianContactTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(int guardianContactKey)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocGuardianContactTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@guardianContactKey", guardianContactKey);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the GuardianContactTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static GuardianContactTable FillDataRecord(IDataRecord myDataRecord)
        {
            GuardianContactTable myGuardianContactTable = new GuardianContactTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("GuardianContactKey")))
            {
                myGuardianContactTable.GuardianContactKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("GuardianContactKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
            {
                myGuardianContactTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("MobileNumber")))
            {
                myGuardianContactTable.MobileNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("MobileNumber"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("HomePhoneNumber")))
            {
                myGuardianContactTable.HomePhoneNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("HomePhoneNumber"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("FaxNumber")))
            {
                myGuardianContactTable.FaxNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("FaxNumber"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("OfficePhoneNumber")))
            {
                myGuardianContactTable.OfficePhoneNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("OfficePhoneNumber"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("OtherNumber")))
            {
                myGuardianContactTable.OtherNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("OtherNumber"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EmailAddress")))
            {
                myGuardianContactTable.EmailAddress = myDataRecord.GetString(myDataRecord.GetOrdinal("EmailAddress"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myGuardianContactTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myGuardianContactTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myGuardianContactTable;
        }
    }

}
